/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteSeparator;
import org.eclipse.gef.palette.PaletteStack;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.ui.palette.FlyoutPaletteComposite.FlyoutPreferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.uml2.uml.Abstraction;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Collaboration;
import org.eclipse.uml2.uml.CollaborationUse;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Realization;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.Usage;

import com.cea.papyrus.core.IPapyrusUIConstants;
import com.cea.papyrus.core.PapyrusMessages;
import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.CreationEntryFactory;
import com.cea.papyrus.core.editor.DiagramPalette;
import com.cea.papyrus.core.pseudoelement.GraphicalLink;
import com.cea.papyrus.core.pseudoelement.InstanceSpecificationLink;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.composite.pseudoelement.RoleBinding;

/**
 * Utility class that creates a GEF Palette.
 * @see CompositePaletteFactory#createPalette()
 */
public final class CompositePaletteFactory extends CreationEntryFactory {

	/** Preference ID used to save the palette location */
	private static final String PALETTE_DOCK_LOCATION = "ShapesEditorPaletteFactory.Location";
	
	/** Preference ID used to save the palette size */
	private static final String PALETTE_SIZE = "ShapesEditorPaletteFactory.Size";
	
	/** Preference ID used to persist the flyout palette's state */
	private static final String PALETTE_STATE = "ShapesEditorPaletteFactory.State";

	/** debug channel */
	private static final int debugChannel = 3;
	
	/**
	 * Creates the palette container containing uml node elements creations tools
	 * @return the palette container containing uml node elements creations tools
	 */
	private static PaletteContainer createShapesDrawer() {
		Debug.debug(null, "Call CompositePaletteFactory.createShapesDrawer()", debugChannel);
		PaletteDrawer componentsDrawer = new PaletteDrawer("Uml Element");

		// list of palette entries to add in the palette
		List<PaletteEntry> entries = new ArrayList<PaletteEntry>();
		try{
			// Class creation
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddClassCommand_Label,
					PapyrusMessages.AddClassCommand_Description,
					Class.class,
					null));
			
			// Instance Specification
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddInstanceSpecificationCommand_Label,
					PapyrusMessages.AddInstanceSpecificationCommand_Description,
					InstanceSpecification.class,
					null));
			
			// Slot
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddSlotCommand_Label,
					PapyrusMessages.AddSlotCommand_Description,
					Slot.class,
					null));
			
			// Collaboration
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddCollaborationCommand_Label,
					PapyrusMessages.AddCollaborationCommand_Description,
					Collaboration.class,
					null));
			
			// Collaboration Use
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddCollaborationUseCommand_Label,
					PapyrusMessages.AddCollaborationUseCommand_Description,
					CollaborationUse.class,
					null));		
			
			// Port
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddPortCommand_Label,
					PapyrusMessages.AddPortCommand_Description,
					Port.class,
					null));
			
			// property
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddPropertyCommand_Label,
					PapyrusMessages.AddPropertyCommand_Description,
					Property.class,
					null));
			
			// Comment
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddCommentCommand_Label,
					PapyrusMessages.AddCommentCommand_Description,
					Comment.class,
					null));
			
			// Constraint
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddConstraintCommand_Label,
					PapyrusMessages.AddConstraintCommand_Description,
					Constraint.class,
					null));
		} catch (Exception e) {
			Debug.log(e);
		}
		componentsDrawer.addAll(entries);
		return componentsDrawer;
	}

	/**
	 * Creates the PaletteRoot and adds all palette elements.
	 * Use this factory method to create a new palette for your graphical editor.
	 * @return a new Diagram palette for the composite structure diagram
	 */
	public static DiagramPalette createPalette() {
		Debug.debug(null,"Call CompositePaletteFactory.createPalette()", debugChannel);
		DiagramPalette palette = new DiagramPalette(IPapyrusUIConstants.COMPOSITE_STRUCTURE_DIAGRAM);
		palette.add(createToolsGroup(palette));
		palette.add(createShapesDrawer());
		
		// Create additionnal menu defined in extensions
		palette.createMenuExtensions();

		return palette;
	}

	/**
	 * Return a FlyoutPreferences instance used to save/load the preferences of a flyout palette.
	 * @return the flyout preferences for the palette
	 */
	static FlyoutPreferences createPalettePreferences() {
		Debug.debug(null,"Call CompositePaletteFactory.createPalettePreferences()", debugChannel);
		return new FlyoutPreferences() {
			private IPreferenceStore getPreferenceStore() {
				return Activator.getDefault().getPreferenceStore();
			}
			
			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#getDockLocation()
			 */
			public int getDockLocation() {
				return getPreferenceStore().getInt(PALETTE_DOCK_LOCATION);
			}
			
			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#getPaletteState()
			 */
			public int getPaletteState() {
				return getPreferenceStore().getInt(PALETTE_STATE);
			}
			
			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#getPaletteWidth()
			 */
			public int getPaletteWidth() {
				return getPreferenceStore().getInt(PALETTE_SIZE);
			}
			
			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#setDockLocation(int)
			 */
			public void setDockLocation(int location) {
				getPreferenceStore().setValue(PALETTE_DOCK_LOCATION, location);
			}
			
			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#setPaletteState(int)
			 */
			public void setPaletteState(int state) {
				getPreferenceStore().setValue(PALETTE_STATE, state);
			}
			
			/* (non-Javadoc)
			 * @see org.eclipse.gef.ui.palette.FlyoutPaletteComposite$FlyoutPreferences#setPaletteWidth(int)
			 */
			public void setPaletteWidth(int width) {
				getPreferenceStore().setValue(PALETTE_SIZE, width);
			}
		};
	}

	/**
	 * Create the "Tools" group.
	 * @param palette the palette where to add created tools group
	 * @return the palette container created
	 */
	private static PaletteContainer createToolsGroup(PaletteRoot palette) {
		Debug.debug(null, "Call CompositePaletteFactory.createToolsGroup()", debugChannel);

		PaletteGroup toolGroup = new PaletteGroup("RelationShips");

		// Add a selection tool to the group
		ToolEntry tool = new PanningSelectionToolEntry();
		toolGroup.add(tool);
		palette.setDefaultEntry(tool);

		// Add a marquee tool to the group
		toolGroup.add(new MarqueeToolEntry());

		// Add a (unnamed) separator to the group
		toolGroup.add(new PaletteSeparator());

		///////////////////////////////////////////////////////////////////////
		// Composite structure diagram compenents
		///////////////////////////////////////////////////////////////////////
		List<PaletteEntry> entries = new ArrayList<PaletteEntry>();
		try {
			///////////////////////////////////////////////////////////////////
			// Begin of entry stack for Dependencies
			///////////////////////////////////////////////////////////////////
			PaletteStack paletteStack = new PaletteStack("", "", null);
			
			// Add Dependency connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddDependencyCommand_Label,
					PapyrusMessages.AddDependencyCommand_Description,
					Dependency.class,
					null));

			// Add Abstraction connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddAbstractionCommand_Label,
					PapyrusMessages.AddAbstractionCommand_Description,
					Abstraction.class,
					null));
			

			// Add Realization connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddRealizationCommand_Label,
					PapyrusMessages.AddRealizationCommand_Description,
					Realization.class,
					null));
			
			
			// Add Usage connection tool
			paletteStack.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddUsageCommand_Label,
					PapyrusMessages.AddUsageCommand_Description,
					Usage.class,
					null));	
			
			entries.add(paletteStack);	
			
			///////////////////////////////////////////////////////////////////
			// End of entry stack for Dependencies
			///////////////////////////////////////////////////////////////////
			
			// Generalization
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddGeneralizationCommand_Label,
					PapyrusMessages.AddGeneralizationCommand_Description,
					Generalization.class,
					null));
			
			// Connector
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddConnectorCommand_Label,
					PapyrusMessages.AddConnectorCommand_Description,
					Connector.class,
					null));
			
			// instance specification link
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddLinkCommand_Label,
					PapyrusMessages.AddLinkCommand_Description,
					InstanceSpecificationLink.class,
					null));
			
			// Role binding
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddRoleBindingCommand_Label,
					PapyrusMessages.AddRoleBindingCommand_Description,
					RoleBinding.class,
					null));
			
			///////////////////////////////////////////////////////////////////
			// Begin of tool for Comment and constraint links
			///////////////////////////////////////////////////////////////////
			
			// Add GraphicalLink
			entries.add(PapyrusPlugin.creationToolFactory.getConnectionCreationToolEntry(
					PapyrusMessages.AddGraphicalLinkCommand_Label,
					PapyrusMessages.AddGraphicalLinkCommand_Description,
					GraphicalLink.class,
					null));	
			
		} catch (Exception e) {
			Debug.log(e);
		}
		// add all elements to the tool group
		toolGroup.addAll(entries);
		
		return toolGroup;
	}

}